package com.example.springboot.controller;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Book;
import com.example.springboot.mapper.BookMapper;
import com.example.springboot.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/book")
public class BookController {
    @Autowired
    private BookMapper bookMapper;
    @Autowired
    private BookService bookService;
    //查询全部
    @GetMapping
    public Result findAll(){
        return Result.success(bookMapper.findAll());
    }
    //新增
    @PostMapping
    public Result addBook(@RequestBody Book book){
        return Result.success(bookMapper.insert(book));
    }
    //上货
    @PostMapping("/updateBook")
    public Result updateNum(@RequestBody Book book){
        return Result.success(bookMapper.updateNum(book));
    }
    //删除
    @DeleteMapping("/{ISBN}")
    public Result delete(@PathVariable Long ISBN){
        return Result.success(bookMapper.deleteByISBN(ISBN));
    }
    //查询
    @GetMapping("/findbyISBN")
    public Result findByISBN(@RequestParam Long ISBN){
        List<Book> all=bookMapper.findByISBN(ISBN);
        return Result.success(all);
    }
    @GetMapping("/findbyauthor")
    public Result findByAuthor(@RequestParam String author){
        List<Book> all=bookMapper.findByAuthor(author);
        return Result.success(all);
    }
    @GetMapping("/findbybname")
    public Result findByBname(@RequestParam String bname){
        List<Book> all=bookMapper.findByBname(bname);
        return Result.success(all);
    }

}
